import { FacilityType } from "../info/GameEnum";

const { ccclass, property } = cc._decorator;

/**
 * 管理所有节点池
 */
@ccclass
export default class NodePoolManager extends cc.Component {

    @property({ type: cc.Prefab, displayName: "采矿提示" })
    public miningTipPrefab: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "信息提示" })
    public inforTipPrefab: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "侦察机" })
    public scout: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "矿工" })
    public minerPrefab: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "平头哥" })
    public badger: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "近防炮子弹" })
    public defenceBullet: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "榴弹炮炮弹" })
    public howitzerBullet: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "火箭炮炮弹" })
    public rocketBullet: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "电磁炮炮弹" })
    public electricBullet: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "照明弹" })
    public flareBullet: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "洲际导弹" })
    public intercontinentalMissileBullet: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "核弹" })
    public nuclearBomb: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "黑洞" })
    public blackHole: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "反导导弹" })
    public mediumRangeAntimissileMissileBullet: cc.Prefab = null;

    //玩家设施
    @property({ type: cc.Prefab, displayName: "侦察机基地" })
    public scoutBase: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "领土扩张器" })
    public domainExpander: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "矿机" })
    public miningMachine: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "近防炮" })
    public defenseCannon: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "榴弹炮基地" })
    public howitzerBase: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "火箭炮基地" })
    public rocketBase: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "照明弹基地" })
    public flareBase: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "电磁炮基地" })
    public electricBase: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "洲际导弹基地" })
    public intercontinentalMissileBase: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "核弹基地" })
    public nuclearBombBase: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "卫星" })
    public satellite: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "风电站" })
    public windPlant: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "光伏板" })
    public photovoltaicPanel: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "核电厂" })
    public nuclearPlant: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "反导雷达" })
    public radar: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "火电站" })
    public thermalPowerPlant: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "黑洞掠夺者" })
    public blackHolePredator: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "黑洞发生器" })
    public blackHoleGenerator: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "蜂刺反导导弹基地" })
    public mediumRangeAntimissileMissile: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "激光器" })
    public laserGun: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "超级激光炮" })
    public superLaserGun: cc.Prefab = null;

    //特效
    @property({ type: cc.Prefab, displayName: "爆炸特效" })
    public explodeEffect: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "电磁爆炸特效" })
    public electricEffect: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "子弹特效" })
    public bulletEffect: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "魔盒特效" })
    public magicBoxEffect: cc.Prefab = null;


    //道具功能
    @property({ type: cc.Prefab, displayName: "防护罩" })
    public facilityHood: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "维修" })
    public facilityRepair: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "回收" })
    public facilityRecycle: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "侦察机储备" })
    public scoutReserve: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "照明弹储备" })
    public flareReserve: cc.Prefab = null;

    private nodePools: Map<string, cc.NodePool> = new Map();
    public static instance: NodePoolManager = null;

    protected onLoad(): void {
        NodePoolManager.instance = this;

        Object.keys(this).forEach(key => {
            this[key] instanceof cc.Prefab && this.createPool(this[key].name);
        })
    }

    createPool(prefabName: string): void {
        if (!this.nodePools.has(prefabName)) {
            const pool = new cc.NodePool();
            this.nodePools.set(prefabName, pool);
        }
    }

    obtain(prefab: cc.Prefab): cc.Node {
        const pool = this.nodePools.get(prefab.name);
        if (!pool) {
            throw new Error(`No pool found for type: ${prefab.name}`);
        }
        let node = pool.get();
        if (!node) node = cc.instantiate(prefab);
        return node;
    }

    recycle(node: cc.Node): void {
        const pool = this.nodePools.get(node.name);
        if (!pool) {
            console.warn(`No pool available to recycle node of type: ${node.name}`);
            return;
        }
        pool.put(node);
    }

    clear() {
        this.nodePools.forEach(item => item.clear())
        this.nodePools.clear();
        this.nodePools = null;
    }

    protected onDestroy(): void {
        NodePoolManager.instance = null;
    }

    getPrefabByFacilityType(facilityType: FacilityType): cc.Prefab {
        let ret: cc.Prefab;
        switch (facilityType) {
            case FacilityType.侦察机基地:
                ret = this.scoutBase;
                break;
            case FacilityType.领土扩张器:
                ret = this.domainExpander;
                break;
            case FacilityType.矿机:
                ret = this.miningMachine;
                break;
            case FacilityType.近防炮:
                ret = this.defenseCannon;
                break;
            case FacilityType.榴弹炮基地:
                ret = this.howitzerBase;
                break;
            case FacilityType.火箭炮基地:
                ret = this.rocketBase;
                break;
            case FacilityType.电磁炮基地:
                ret = this.electricBase;
                break;
            case FacilityType.照明弹基地:
                ret = this.flareBase;
                break;
            case FacilityType.洲际导弹基地:
                ret = this.intercontinentalMissileBase;
                break;
            case FacilityType.核弹基地:
                ret = this.nuclearBombBase;
                break;
            case FacilityType.卫星:
                ret = this.satellite;
                break;
            case FacilityType.风电站:
                ret = this.windPlant;
                break;
            case FacilityType.光伏板:
                ret = this.photovoltaicPanel;
                break;
            case FacilityType.核电厂:
                ret = this.nuclearPlant;
                break;
            case FacilityType.反导雷达:
                ret = this.radar;
                break;
            case FacilityType.火电站:
                ret = this.thermalPowerPlant;
                break;
            case FacilityType.黑洞掠夺者:
                ret = this.blackHolePredator;
                break;
            case FacilityType.黑洞发生器:
                ret = this.blackHoleGenerator;
                break;
            case FacilityType.蜂刺反导导弹基地:
                ret = this.mediumRangeAntimissileMissile;
                break;
            case FacilityType.超级激光炮:
                ret = this.superLaserGun;
                break;
            case FacilityType.激光器:
                ret = this.laserGun;
                break;
        }
        return ret;
    }

}
